package com.syj.collect.Present;

import android.net.Uri;
import android.util.Log;

import com.syj.collect.base.BasePresent;
import com.syj.collect.entry.UpdataPass;
import com.syj.collect.http.ApiServise;
import com.syj.collect.http.BaseObserver;
import com.syj.collect.http.BaseRequest;
import com.syj.collect.model.MyUserActivity;
import com.syj.collect.utils.GsonUtil;
import com.syj.collect.utils.SPUtil;
import com.syj.collect.utils.ToastMgr;
import com.syj.collect.view.CollectActivityView;
import com.syj.collect.view.UserActivityView;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import retrofit2.http.Multipart;

public class UserPresent extends BasePresent<UserActivityView> {
    public void upLoadFile(String str) {

        File file = new File(str);
        // create RequestBody instance from file

        RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);

        MultipartBody.Part part = MultipartBody.Part.createFormData("file", file.getName(), requestBody);

        BaseRequest.getInstance().getApiServise().upLoadImg(part).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<String>() {
                    @Override
                    public boolean onSuccess(String value) {
//                        Log.e("files", value);

                        try {
                            JSONObject jsonObject = new JSONObject(value);
//                            Log.e("结果 :", value);
                            String msg = jsonObject.getString("msg");
                            int code = jsonObject.getInt("code");
                            if (code == 0) {
                                view.initSuccess(value);
                            } else {
                                view.initFail(msg);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        return false;
                    }

                    @Override
                    public void onFailure(Throwable e) throws Exception {
//                        Log.e("files", e.toString());
                        view.initFail(e.toString());
                    }

                    @Override
                    public void onEnd() {

                    }
                });
    }
    public void setNewPassword(String newPassword, String password) {
        HashMap hashMap=new HashMap();
        hashMap.put("newPassword",newPassword);
        hashMap.put("password",password);

        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), GsonUtil.gsonString(hashMap));
        BaseRequest.getInstance().getApiServise().password(body)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<String>() {
                    @Override
                    public boolean onSuccess(String value) {
                        view.updataPas(value);
                        return false;
                    }

                    @Override
                    public void onFailure(Throwable e) throws Exception {
                        view.initFail("网络异常");
                    }

                    @Override
                    public void onEnd() {

                    }
                });
    }






}